package com.huowolf.oa.view.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.huowolf.oa.domain.Role;
import com.huowolf.oa.service.RoleService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


@Controller
@Scope("prototype")
public class RoleAction extends ActionSupport implements ModelDriven<Role>{

	private Role roleModel;
	
	@Override
	public Role getModel() {
		 if(roleModel == null){
			 roleModel = new Role();
	       }
		return roleModel;
	}
	
	@Resource
	private RoleService roleService;
	/**
	 * 列表
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		List<Role> roleList = roleService.findAll(); 
		ActionContext.getContext().put("roleList", roleList);
		return "list";
	}
	
	/**
	 * 删除
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		roleService.delete(roleModel.getId());
		return "toList";
	}
	
	/**
	 * 添加页面
	 * @return
	 * @throws Exception
	 */
	public String addUI() throws Exception {
		return "saveUI";
	}
	
	/**
	 * 添加
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception {
		/*		Role role = new Role();
		role.setName(roleModel.getName());
		role.setDescription(roleModel.getDescription());*/
		
		roleService.save(roleModel);
		return "toList";
	}
	
	/**
	 * 修改页面
	 * @return
	 * @throws Exception
	 */
	public String editUI() throws Exception {
		System.out.println(roleModel.getId());
		//准备回显的数据
		Role role = roleService.getById(roleModel.getId());
		ActionContext.getContext().getValueStack().push(role);
		//this.name = role.getName();
		//this.description = role.getDescription();
		return "saveUI";
	}
	
	/**
	 * 修改
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {
		//1.从数据库中获取原对象
		Role role = roleService.getById(roleModel.getId());
		//2.设置要修改的属性
		role.setName(roleModel.getName());
		role.setDescription(roleModel.getDescription());
		//3.更新到数据库
		roleService.update(role);
		return "toList";
		
	}

}
